//
//  YRRangeView.swift
//  YRProject
//
//  Created by 薛永伟 on 2019/10/8.
//  Copyright © 2019 XueYongWei. All rights reserved.
//

import UIKit

class YRRangeView: CodeLayoutView {

    /// 操作位置
    enum ActionPosition {
        /// 左侧
        case Left
        /// 右侧
        case Right
    }
    
    /// 操纵按钮的宽度
    static let actionBtnWidth = 20.scaleLength
    
    /// 操作位置
    var actionPosition:ActionPosition = .Right
    
    /// 轨道view
    lazy var trackView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.init(rgb: 0x979797)
        return view
    }()

    lazy var blockView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.init(rgb: 0xFFD500)
        return view
    }()
    
    /// 左侧操作按钮
    lazy var leftBtn: UIButton = {
        let view = UIButton.init(type: .custom)
        
        view.imageView?.contentMode = .scaleAspectFit
        view.imageView?.clipsToBounds = true
        view.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        view.setTitle("00:00", for: .normal)
        view.setTitleColor(UIColor.init(rgb: 0x999999), for: .normal)
        view.setTitleColor(UIColor.init(rgb: 0xFFD500), for: .highlighted)
        view.setImage(UIImage.init(named: "music_add_slid_unselect"), for: .normal)
        view.setImage(UIImage.init(named: "music_add_slid_select"), for: .highlighted)
        view.setImagePosition(.bottom, spacing: 1)
        view.isUserInteractionEnabled = false
        return view
    }()
    
    /// 右侧操纵按钮
    lazy var rightBtn: UIButton = {
        let view = UIButton.init(type: .custom)
        
        view.imageView?.contentMode = .scaleAspectFit
        view.imageView?.clipsToBounds = true
        view.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        view.setTitle("00:00", for: .normal)
        view.setTitleColor(UIColor.init(rgb: 0x999999), for: .normal)
        view.setTitleColor(UIColor.init(rgb: 0xFFD500), for: .highlighted)
        view.setImage(UIImage.init(named: "music_add_slid_unselect"), for: .normal)
        view.setImage(UIImage.init(named: "music_add_slid_select"), for: .highlighted)
        view.setImagePosition(.bottom, spacing: 1)
        view.isUserInteractionEnabled = false
        return view
    }()
    
    
    override func codeCustomSubviews() {
        
        addSubview(trackView)
        trackView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(6.scaleLength)
            make.left.equalToSuperview().offset(18.scaleLength)
            make.right.equalToSuperview().offset(-18.scaleLength)
            make.height.equalTo(2.scaleLength)
        }
        
        
        addSubview(leftBtn)
        leftBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(0)
            make.centerY.equalToSuperview()
            make.width.equalTo(36.scaleLength)
            make.height.equalTo(55.scaleLength)
        }
        
        addSubview(rightBtn)
        rightBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(100.scaleLength)
            make.centerY.equalToSuperview()
            make.width.equalTo(36.scaleLength)
            make.height.equalTo(55.scaleLength)
        }
        
        addSubview(blockView)
        blockView.snp.makeConstraints { (make) in
            make.centerY.equalTo(trackView.snp.centerY)
            make.left.equalTo(leftBtn.snp.right).offset(-18.scaleLength)
            make.right.equalTo(rightBtn.snp.left).offset(18.scaleLength)
            make.height.equalTo(2.scaleLength)
        }
        
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        guard event?.touches(for: self)?.count == 1 else {
            // 仅支持单指手势
            return
        }
        
        
        guard let touch = (touches as NSSet).anyObject() as? UITouch else {
            return
        }
        
        let position = touch.location(in: self)
        
        if self.leftBtn.frame.contains(position) {
            print("左侧")
            actionPosition = .Left
            self.bringSubviewToFront(leftBtn)
        }else if rightBtn.frame.contains(position){
            print("右侧")
            actionPosition = .Right
            self.bringSubviewToFront(rightBtn)
        }else{
            print("无效")
            return
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        
        guard let touch = (touches as NSSet).anyObject() as? UITouch else {
            return
        }
        
        // 计算基于window的位置偏移
        let previousLocation = touch.previousLocation(in: self.window)
        let currentLocation = touch.location(in: self.window)
        
        // 向右移动
        let deltX = currentLocation.x - previousLocation.x
        
        if actionPosition == .Left {//左侧按钮往右移动
            
            var aFrame = self.leftBtn.frame
            aFrame.origin.x += deltX
            if aFrame.origin.x < 0 { aFrame.origin.x = 0}
            if aFrame.maxX > self.rightBtn.frame.minX { aFrame.origin.x = rightBtn.frame.minX - aFrame.size.width }

            leftBtn.snp.updateConstraints { (make) in
                make.left.equalToSuperview().offset(aFrame.origin.x)
            }
        }
        else {
            
            var aFrame = self.rightBtn.frame
            aFrame.origin.x += deltX
            if aFrame.origin.x < 0 { aFrame.origin.x = 0}
            if aFrame.origin.x < self.leftBtn.frame.maxX { aFrame.origin.x = leftBtn.frame.maxX }
            if aFrame.maxX > self.bounds.maxX { aFrame.origin.x = bounds.maxX - aFrame.width}

            rightBtn.snp.updateConstraints { (make) in
                make.left.equalToSuperview().offset(aFrame.origin.x)
            }
        }

    }
    
}

